//
//  DownloadViewController.m
//  yonghu
//
//  Created by 浪尖渝力 on 2018/2/25.
//  Copyright © 2018年 com.DJS. All rights reserved.
//

#import "DownloadViewController.h"
#import "DownloadView.h"
@interface DownloadViewController ()
@property(nonatomic,strong)DownloadView *aView;
@property(nonatomic,strong)UIView *bottomView;
@end

@implementation DownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的下载";
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    
    _aView=[DownloadView new];
    [self.view addSubview:_aView];
    [_aView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    _aView.selectArr=[NSMutableArray arrayWithCapacity:0];
    [_aView creatView];
    _aView.viewController=self;
}

-(void)BianJi:(UIBarButtonItem *)btn{
    if ([btn.title isEqualToString:@"编辑"]) {
        
        [_aView.selectArr removeAllObjects];
        [_aView.tableView setEditing:YES animated:YES];
        [_aView.tableView setFrame:CGRectMake(0, NAV_HEIGHT,_mainW ,_mainH-NAV_HEIGHT-50)];
        [self creatSelecBtn];
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    }
    else{
     
        [_aView.tableView setEditing:NO animated:YES];
        [_aView.tableView setFrame:CGRectMake(0, NAV_HEIGHT,_mainW ,_mainH-NAV_HEIGHT)];
        
        [_aView.selectArr removeAllObjects];
        [_bottomView removeFromSuperview];
        self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
    }
    
    
}
-(void)creatSelecBtn{
    _bottomView=[UIView new];
    [self.view addSubview:_bottomView];
    [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    _bottomView.backgroundColor=[UIColor whiteColor];
    
    UIView *xianView=[UIView new];
    [_bottomView addSubview:xianView];
    [xianView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo(1);
    }];
    xianView.backgroundColor=_LightGrey;
    
    UIButton *btn=[UIButton new];
    [_bottomView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    [btn setTitleColor:ColorWithRGB(149, 149, 149) forState:0];
    [btn setTitle:@"全选" forState:0];
    btn.titleLabel.font=[UIFont systemFontOfSize:16];
    [btn addTarget:self action:@selector(qunXuan:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *btn2=[UIButton new];
    [_bottomView addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.top.mas_equalTo(0);
        make.width.mas_equalTo(_mainW/2);
    }];
    btn2.backgroundColor=ColorWithRGB(251, 83, 86);
    [btn2 setTitleColor:[UIColor whiteColor] forState:0];
    [btn2 setTitle:@"删除" forState:0];
    btn2.titleLabel.font=[UIFont systemFontOfSize:16];
    [btn2 addTarget:self action:@selector(shanchu) forControlEvents:UIControlEventTouchUpInside];
}
-(void)qunXuan:(UIButton *)btn{
    if ([btn.titleLabel.text isEqualToString:@"全选"]) {
        [btn setTitle:@"取消全选" forState:0];
            for (int i = 0; i < _aView.tableArr.count; i++) {
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
                NSDictionary *dic=_aView.tableArr[i];
                [_aView.selectArr addObject:dic];
            }
        
    }
    else{
        [btn setTitle:@"全选" forState:0];
            for (int i = 0; i < _aView.tableArr.count; i++) {
                
                NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
                [_aView.tableView deselectRowAtIndexPath:indexPath animated:YES];
                NSDictionary *dic=_aView.tableArr[i];
                [_aView.selectArr removeObject:dic];
            }
    }
}
-(void)shanchu{
    
    
    
    NSArray *xiaArr = [NSKeyedUnarchiver unarchiveObjectWithFile:_xiaZai];
    
    NSMutableArray *typeArr=[NSMutableArray arrayWithCapacity:0];
    [typeArr addObjectsFromArray:xiaArr];
    
    for (int j=0; j<_aView.selectArr.count; j++) {
        NSDictionary *dic2=_aView.selectArr[j];
        NSString *file=[dic2 objectForKey:@"filePath"];
        if ([self deleteFile:file]) {
            [typeArr removeObject:dic2];
        }
    }

    [NSKeyedArchiver archiveRootObject:typeArr toFile:_xiaZai];
    [ZQTools cleanViewSubviews:_aView];
    [_aView creatView];
    [_bottomView removeFromSuperview];
     self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(BianJi:)];
}
// 删除沙盒里的文件
-(BOOL)deleteFile:(NSString *)file{
    
    
    NSString *Path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSString *filename=[Path stringByAppendingPathComponent:file];

    NSFileManager* fileManager=[NSFileManager defaultManager];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:filename];
    
    if (!blHave) {
        return YES;
        
    }else {
        NSLog(@" have");
        BOOL blDele= [fileManager removeItemAtPath:filename error:nil];
        if (blDele) {
            return YES;
           
        }else {
            return NO;
            
        }
    }
    return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
